package com.hinnotek.warehouse.ui

import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.hinnotek.warehouse.R
import com.hinnotek.warehouse.databinding.ActivityWarehouseQueryBinding
import com.hinnotek.warehouse.databinding.ItemQueryWarehouseLayoutBinding
import com.lee.common.adapter.binding.SimpleBindingAdapter
import com.lee.common.base.BaseActivity
import dagger.hilt.android.AndroidEntryPoint
import com.hinnotek.warehouse.BR
import com.hinnotek.warehouse.bean.WareHouseInfoBean
import com.hinnotek.warehouse.viewmodel.WarehouseViewModel
import com.lee.common.ext.init
import com.lee.common.observer.DefaultObserver
import com.lee.common.utils.ACTIVITY_WAREHOUSE_DETAILS
import com.lee.common.utils.ACTIVITY_WAREHOUSE_QUERY
import com.lee.common.utils.ACTIVITY_WAREHOUSE_STOCK_NUMBER_DETAIL
import com.lee.common.utils.Skipping

/**
 * 盘库管理信息查询页面
 */
@Route(path = ACTIVITY_WAREHOUSE_QUERY)
@AndroidEntryPoint
class WarehouseQueryActivity : BaseActivity<ActivityWarehouseQueryBinding>() {

    private lateinit var adapter: SimpleBindingAdapter<WareHouseInfoBean, ItemQueryWarehouseLayoutBinding>
    private val viewModel by viewModels<WarehouseViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_warehouse_query)
        setTitle("盘库管理")
        bindingView.vm1 = viewModel
        bindingView.executePendingBindings()
        viewModel.attachLoading(loadingState)
        viewModel.dialogMsg.observe(this, DefaultObserver(this))
        viewModel.autoRefresh()
    }

    override fun initVew() {
        adapter = SimpleBindingAdapter(
            R.layout.item_query_warehouse_layout,
            ArrayList(),
            BR.orderGrabbingHallData
        )
        bindingView.recycler.init()
        bindingView.recycler.adapter = adapter
        emptyView.adapter = adapter
        viewModel.isEmpty.observe(this, Observer {
            emptyView.values(it)
        })
        emptyView.getErrView().setOnClickListener {
            viewModel.refresh()
        }

        adapter.addListener { binding, itemData ->
            binding.root.setOnClickListener {
                Skipping.start(ACTIVITY_WAREHOUSE_DETAILS,"wareData",itemData,this)
            }
            binding.numDetail.setOnClickListener {
                Skipping.start(ACTIVITY_WAREHOUSE_STOCK_NUMBER_DETAIL,"wareId",itemData.wareId,this)
            }
        }

        viewModel.warehousePage.observe(this, Observer {
            adapter.addAll(it, true)
        })

    }

    override fun setData() {

    }
}